#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define MAIL_PROGRAM "mailx"
#define MAIL_TO_MAX_LEN 32
#define MAIL_SUBJECT_MAX_LEN 128
#define MAIL_CONTENT_MAX_LEN 2048
#define MAIL_CMD_MAX_LEN MAIL_TO_MAX_LEN + MAIL_SUBJECT_MAX_LEN + MAIL_CONTENT_MAX_LEN + 256

int send_mail(char *_to, char *_subject, char *_content)
{
	if (_to == NULL ||_subject == NULL || _content == NULL) {
		return 1;
	}
	
	char to[MAIL_TO_MAX_LEN], subject[MAIL_SUBJECT_MAX_LEN], content[MAIL_CONTENT_MAX_LEN];	     
	strcpy(to, _to);
	to[MAIL_TO_MAX_LEN - 1] = '\0';
	strcpy(subject, _subject);
	subject[MAIL_SUBJECT_MAX_LEN - 1] = '\0';
	strcpy(content, _content);
	content[MAIL_CONTENT_MAX_LEN - 1] = '\0';
	
	char cmd[MAIL_CMD_MAX_LEN];
	sprintf(cmd, "echo \"%s\" | %s -s \"%s\" %s", content, MAIL_PROGRAM, subject, to);
	cmd[MAIL_CMD_MAX_LEN - 1] = '\0';
	printf("%s\n", cmd);

	int ret = 0;
	ret = system(cmd);

	return ret;
}

int main()
{
	send_mail("qthdaidai@qq.com", "TEST", "CONTENT");	

	return 0;
}
